//
//  BackMoneyViewController.m
//  JinChengYun
//
//  Created by 金橙云 on 2017/3/9.
//  Copyright © 2017年 金橙云. All rights reserved.
//

#define KA 20
#define KB (B_WIDTH-100)/4
#import "BackMoneyViewController.h"

#import "TodoHttpManager.h"
#import "SalesMoneyCell.h"

#import "MoneyFooter.h"

#import "BackMoneyHeader.h"
#import "AllLableCell.h"
#import "HuiMoneyModel.h"
#import "MJExtension.h"

#import "SVPullToRefresh.h"
@interface BackMoneyViewController ()<UITableViewDelegate,UITableViewDataSource>
{
    UIView *oneView;
    UISegmentedControl *segmented;
    NSArray *zhiA;
    NSInteger _segmentIndex;//记录点击的第几个segment
    NSMutableArray *_zhushouArr;
    NSMutableArray *_zhushouqiankuanArr;
}


@property(nonatomic,strong)NSMutableDictionary *zoDic;

@property(nonatomic,strong)UITableView *tabView;
@property(nonatomic,strong)BackMoneyHeader *backHeader;
@property(nonatomic,strong)MoneyFooter *moneyFoot;

@property(nonatomic,strong)UILabel *hengLab;//回款动态


@end

@implementation BackMoneyViewController

-(void)viewWillAppear:(BOOL)animated
{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didTDManagerFirstMoney:) name:TDManagerFirstMoney object:nil];
    
   [_manager userManagerFirstMoneyDynamic];
}

-(void)didTDManagerFirstMoney:(NSNotification *)noti
{
    if (noti.object !=0)
    {
        _zoDic = [noti.object objectForKey:@"data"];
        NSLog(@"------------总的数据%@",noti.object);

        //*************************回款*************
        //主售房源的 回款
        NSMutableArray *arr1 = [NSMutableArray arrayWithObjects:_zoDic[@"fangkuan"],_zoDic[@"daikuan"],_zoDic[@"zongji"], nil];
        //商业房源的 回款
        NSMutableArray *arr2 = [NSMutableArray arrayWithObjects:_zoDic[@"sy_fangkuan"],_zoDic[@"sy_daikuan"],_zoDic[@"sy_zongji"], nil];
        // 配套房源的 回款
        NSMutableArray *arr3 = [NSMutableArray arrayWithObjects:_zoDic[@"chewei_fangkuan"],_zoDic[@"ccs_fangkuan"], nil];
        _zhushouArr = [NSMutableArray arrayWithObjects:arr1,arr2,arr3, nil];
        
        //****************************欠款**********
        //主售房源的 欠款
        NSMutableArray *arr11 = [NSMutableArray arrayWithObjects:_zoDic[@"zs_qian"], nil];
        //商业房源的 欠款
        NSMutableArray *arr22 = [NSMutableArray arrayWithObjects:_zoDic[@"sy_qian"], nil];
        //配套房源的 欠款
        NSMutableArray *arr33 = [NSMutableArray arrayWithObjects:_zoDic[@"pt_qian"], nil];

        _zhushouqiankuanArr = [NSMutableArray arrayWithObjects:arr11,arr22,arr33, nil];

        [_tabView reloadData];
    }
}

-(void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:TDManagerFirstMoney object:nil];
}
-(BackMoneyHeader *)backHeader
{
    
    
    if (_backHeader)
    {
        return _backHeader;
    }
    
    _backHeader = [[BackMoneyHeader alloc] initWithFrame:CGRectMake(0, 64, B_WIDTH, 530)];
 
    [self.view addSubview:_backHeader];
    
    
    return _backHeader;
}

-(MoneyFooter *)moneyFoot
{
    if (_moneyFoot)
    {
        return _moneyFoot;
    }
    _moneyFoot = [[MoneyFooter alloc] initWithFrame:CGRectMake(0, 5, B_WIDTH, 230)];
  
    [self.view addSubview:_moneyFoot];
    return _moneyFoot;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    _zoDic = [NSMutableDictionary dictionary];
    _manager = [TodoHttpManager sharedManager];
    self.view.backgroundColor = BgColor;
    _tabView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, B_WIDTH, B_HEIGHT-LL_StatusBarAndNavigationBarHeight-45-LL_TabbarHeight) style:UITableViewStylePlain];
    _tabView.delegate = self;
    _tabView.dataSource = self;
    _tabView.rowHeight = 30;
    _tabView.tableHeaderView = self.backHeader;
    _tabView.tableFooterView = self.moneyFoot;
    [self.view addSubview:_tabView];

    __weak __typeof(self) wself = self;
    _backHeader.returnTextBlock = ^(NSInteger segmentIndex){
        NSLog(@"segmentIndex=dddd==%d",(int)segmentIndex);//012
        _segmentIndex = segmentIndex;
        //循环引用
        [wself.tabView reloadData];
        
        //数据请求 去赋值//////**
        
    };
    _segmentIndex = 0;
    
    
    _tabView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(junJiaRefresh)];
 
}
-(void)junJiaRefresh
{
     [_manager userManagerFirstMoneyDynamic];
    [self.tabView.mj_header endRefreshing];
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (_segmentIndex==2) {
        return 2;
    }else{
        return 3;

    }
    
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellA = @"";
    

        cellA = @"CellN";
        AllLableCell *cell = [tableView cellForRowAtIndexPath:indexPath];
        if (!cell)
        {
            cell = [[AllLableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellA];
        }
        if ([cell isKindOfClass:[AllLableCell class]])
        {
            
            if (_segmentIndex==2) {//配套房源
                NSArray *arr = @[@"车位",@"储藏室"];
                NSMutableDictionary *tmpDic = _zhushouArr[_segmentIndex][indexPath.row];
                
                [cell setupAllCellWithOneLab:arr[indexPath.row] withTwoLab:[NSString stringWithFormat:@"%@",tmpDic[@"day"]] withThreeLab:[NSString stringWithFormat:@"%@",tmpDic[@"month"]] withForLab:[NSString stringWithFormat:@"%@",tmpDic[@"all"]]];
            }else{
                NSArray *arr = @[@"实收房款",@"贷款到账",@"总计"];
                NSMutableDictionary *tmpDic = _zhushouArr[_segmentIndex][indexPath.row];
                
                [cell setupAllCellWithOneLab:arr[indexPath.row] withTwoLab:[NSString stringWithFormat:@"%@",tmpDic[@"day"]] withThreeLab:[NSString stringWithFormat:@"%@",tmpDic[@"month"]] withForLab:[NSString stringWithFormat:@"%@",tmpDic[@"all"]]];
            }

        }
        

        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
        return cell;
   
}

- (void)tableView:(UITableView*)tableView willDisplayCell:(UITableViewCell*)cell
forRowAtIndexPath:(NSIndexPath*)indexPath
{
    for (UIView* subView in cell.contentView.subviews) {
        [subView removeFromSuperview];
    
    }

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
